﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slb.Ocean.Petrel.UI;
using Slb.Ocean.Petrel;
using Slb.Ocean.Petrel.DomainObject;

namespace Public.Ocean.Petrel.Extensions.DomainObject
{
    public static class PropertyVersionExtensions
    {
        /// <summary>
        /// Gets the template for the given version.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <returns></returns>
        public static ITemplate GetTemplate(this PropertyVersion version)
        {
            return PetrelUnitSystem.PropertyVersionService.FindTemplate(version);
        }

        /// <summary>
        /// Gets the template for the given version.
        /// </summary>
        /// <param name="version">The version.</param>
        /// <returns></returns>
        public static IDictionaryTemplate GetTemplate(this DictionaryPropertyVersion version)
        {
            return PetrelUnitSystem.DictionaryPropertyVersionService.FindTemplate(version);
        }

        /// <summary>
        /// Finds or creates a version in the global container.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <returns></returns>
        public static PropertyVersion CreateGlobalVersion(this ITemplate template)
        {
            return PetrelUnitSystem.PropertyVersionService.FindOrCreate(PetrelUnitSystem.PropertyVersionService.GetGlobalPropertyVersionContainer(), template);
        }

        /// <summary>
        /// Finds or creates a version in the global container.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <returns></returns>
        public static DictionaryPropertyVersion CreateGlobalVersion(this IDictionaryTemplate template)
        {
            return PetrelUnitSystem.DictionaryPropertyVersionService.FindOrCreate(PetrelUnitSystem.DictionaryPropertyVersionService.GetGlobalPropertyVersionContainer(), template);
        }

        /// <summary>
        /// Finds or creates a version for the given container.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <param name="container">The container.</param>
        /// <returns></returns>
        public static PropertyVersion CreateVersionFor(this ITemplate template, IPropertyVersionContainer container)
        {
            return PetrelUnitSystem.PropertyVersionService.FindOrCreate(container, template);
        }

        /// <summary>
        /// Finds or creates a version for the given container.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <param name="container">The container.</param>
        /// <returns></returns>
        public static DictionaryPropertyVersion CreateVersionFor(this IDictionaryTemplate template, IPropertyVersionContainer container)
        {
            return PetrelUnitSystem.DictionaryPropertyVersionService.FindOrCreate(container, template);
        }

        /// <summary>
        /// Gets a property by name.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static PropertyVersion GetProperty(this IPropertyVersionContainer container, string name)
        {
            foreach (var property in container.PropertyVersions)
            {
                if (property.Name == name) return property;
            }
            return null;
        }

        /// <summary>
        /// Gets a dictionary property by name.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static DictionaryPropertyVersion GetDictionaryProperty(this IPropertyVersionContainer container, string name)
        {
            foreach (var property in container.DictionaryPropertyVersions)
            {
                if (property.Name == name) return property;
            }
            return null;
        }
    }
}
